package com.crm.properties;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.SetBucketLoggingRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class OssAliyunAutoConfig {

    private final AliyunProperties aliyunProperties;

    @Bean
    public OSS ossClient(){
        log.info("-----------------开始创建OSSClient--------------------");
        OSS ossClient = new OSSClientBuilder().build(aliyunProperties.getEndpoint(),
                aliyunProperties.getAccessKeyId(), aliyunProperties.getAccessKeySecret());
        //判断容器是否存在,不存在就创建
        if (!ossClient.doesBucketExist(aliyunProperties.getBucketName())) {
            ossClient.createBucket(aliyunProperties.getBucketName());
            CreateBucketRequest createBucketRequest = new CreateBucketRequest(aliyunProperties.getBucketName());
            //设置问公共可读
            createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
            ossClient.createBucket(createBucketRequest);
        }

        //添加客户端访问日志
        SetBucketLoggingRequest request = new SetBucketLoggingRequest(aliyunProperties.getBucketName());
        // 设置存放日志文件的存储空间。
        request.setTargetBucket(aliyunProperties.getBucketName());
        // 设置日志文件存放的目录。
        request.setTargetPrefix(aliyunProperties.getBucketName());
        ossClient.setBucketLogging(request);

        log.info("-----------------结束创建OSSClient--------------------");
        return ossClient;
    }


}
